// pages/address/address.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo:{},
        addressList:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        var _this = this;
        const userinfo = wx.getStorageSync("userInfo");
        this.setData({
          userInfo: userinfo
        });
        console.log(_this.data.userInfo.userId)
        if(_this.data.userInfo==''){
          wx.showToast({
            title: '请先登录',
            icon: 'success',
            duration: 2000
          })
        }else{
        _this.getAddress()
        }
    },
        /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var _this = this;
        const userinfo = wx.getStorageSync("userInfo");
        this.setData({
          userInfo: userinfo
        });
        console.log(_this.data.userInfo.userId)
        if(_this.data.userInfo==''){
          wx.showToast({
            title: '请先登录',
            icon: 'success',
            duration: 2000
          })
        }else{
        _this.getAddress()
        }

    },

     //获取地址数据
  getAddress: function () {
    var _this = this
    console.log(_this.data.addressList);
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: 'http://43.139.97.111:8080/address/queryByUserId?userId=' + _this.data.userInfo.userId,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        console.log(res.data);
        if (res.statusCode == 200 && res.data.data!='') {
          _this.setData({
            addressList: res.data.data,
          })
          _this.data.address = _this.data.addressList[0];
        } else {
          _this.setData({
          })
        }
      }
    })
    setTimeout(() => {
      _this.setData({
        address: _this.data.address
      })
    }, 200)
    wx.hideLoading()
  },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    //默认地址选中点击事件
    clickDefault(e){
        var index = e.currentTarget.dataset.index;
        this.data.addressList.forEach(function(v){
            v.isDefault = false;
        })
        this.data.addressList[index].isDefault = true;
        //将默认选中的放到第一位
        var address = this.data.addressList.splice(index,1)[0];
        this.data.addressList = [address, ...this.data.addressList];
        // 正常地址中 按最后更新时间排序
        this.setData({
            addressList: this.data.addressList
        })
    },
    //删除点击事件
    clickDelete(e){
        var _this = this
        var index = e.currentTarget.dataset.index;
        let id = this.data.addressList[index].addressId
        this.data.addressList.splice(index, 1);
        this.setData({
            addressList: this.data.addressList
        })
        wx.showLoading({
            title: '加载中',
            mask: true
          })
          wx.request({
            url: 'http://43.139.97.111:8080/address/deleteById?id=' + id,
            header: {
              'content-type': 'application/json' // 默认值
            },
            method:"DELETE",
            success(res) {
              console.log(res.data);
              if (res.statusCode == 200) {
                _this.getAddress()
              } else {

              }
            }
          })

    },
    //编辑点击事件
    clickEdit(e){
        var index = e.currentTarget.dataset.index;
        var address = this.data.addressList[index];
        wx.navigateTo({
          url: '/pages/address/edit?address=' + JSON.stringify(address),
        })
    },
    //添加点击事件
    clickAdd(e){
        wx.navigateTo({
            url: '/pages/address/edit',
          })
    },
    backPay(e) {
        let pages = getCurrentPages();
        let prevpage = pages[pages.length - 2];//上一个页面
        console.log(prevpage.route)
        var _this = this;
        const idx = e.currentTarget.dataset.index;
        console.log(_this.data.addressList[idx])
        if(prevpage.route=='pages/pay/pay'){
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevpage.setData({
            address: _this.data.addressList[idx],
            haveAddress:true
        })
        wx.navigateBack({
            delta: 1,
            success: (res) => { },
            fail: (res) => { },
            complete: (res) => { },
        })
        }
    },    

})